<?php


namespace app\mobile\controller;
use app\BaseController;
use app\common\JSSDK;
use app\console\model\Article;
use app\model\Member;
use app\console\model\Category;
use think\facade\Request;
use think\facade\Db;
use think\facade\Session;
use think\facade\View;
use think\facade\Route;
use think\facade\Cookie;

class Base extends BaseController
{

    protected $cookie_time = 86400 * 365;
    protected $where = [];
    protected $order = ['sort' => 'asc', 'id' => 'desc'];
    protected $status = [['status', '=', 1]];
    protected $index_status = [['index_status', '=', 1], ['status', '=', 1]];
    protected $member_id = 0;
    protected $param = [];
    protected $unit_id = 0;
    public function initialize()
    {

//        halt($this->in(3,5));
////        $signPackage = (new JSSDK())->getSignPackage();
////        View::assign('signPackage',$signPackage);
//        $i = 1;
//        do{
//            $i++;
//            if($i == 3){
//                continue;
//            }
//            echo $i;
//
//        }while($i != 10);

        $this->member_id = Cookie::get('member_id') ?? 0;
        $this->param = Request::param();
        $this->param['member_id'] = $this->member_id;
        if(!empty(input('unit_id'))){

            Session::set('unit_id',input('unit_id'));

            Session::save();
        }

        $unit_id = Session::get('unit_id');

        $this->unit_id = $unit_id;


//        echo 4;

        $actions = [
            'Order'=> ['create'],
            'Index'=>['index']
        ];

        foreach ($actions as $k => $v) {
            if ($k == Request::controller()) {
                if (in_array(Request::action(), $v)) {

                    if($this->member_id){
//                        $member = Member::where('id', $this->member_id)->find();
//                        if (empty($member)) {
//                            if (!Request::isAjax()) {
//                                $url = url('Member/login');
//                                redirect($url)->send();
//                                die;
//                            } else {
//                                echo json_encode(['code' => 201, 'message' => '去登录']);
//                                exit;
//                            }
//                        }

                    }else{
                        if (!Request::isAjax()) {
                            $url = url('Member/login');
                            redirect($url)->send();
                            die;
                        } else {
                            echo json_encode(['code' => 201, 'message' => '去登录']);
                            exit;
                        }
                    }


                }
            }
        }
//        if($this->member_id){
//            $member = Member::where('id', $this->member_id)->find();
//
//            if (empty($member)) {
//
//            }
//        }else{
//
//        }

    }
    //1-2+n
    public function in($i,$n)
    {

        if ($n == $i) {
            dump($i);
            return $i;

        } else {

            dump($i);
            return $i + $this->in($i + 1,$n);
        }
    }

    public function wechat_sign(){
        $signPackage = (new JSSDK())->getSignPackage();

        return json(['code'=>200,'message'=>'签名','data'=>$signPackage]);
    }
    public function upload()
    {
        $module = app('http')->getName();
        $file = Request::file('single_pic');
        $param = Request::param();
        if (Request::action() == 'batch_create') {
            $savename = \think\facade\Filesystem::disk('public')->putFileAs($module, Request::file('multi_pic'), $file->getOriginalName());
            return json_encode(['code' => 0, 'img' => $savename]);
        } else {
            if (!empty($param['rule'])) {
                return json_encode(['code' => 0, 'img' => uploadOneOrigin('file_data')]);
            } else {
                return json(['code' => 0, 'img' => uploadOne('single_pic')]);
            }

        }
    }

    public function upload_member()
    {

        $module = app('http')->getName();
        $file = Request::file('single_pic');
        $headimgrul = uploadOne('single_pic');
        $ret = (new \app\model\Member())->where('id',Cookie::get('member_id'))->update(['headimgurl'=>$headimgrul]);

        $param = Request::param();
        if (Request::action() == 'batch_create') {
            $savename = \think\facade\Filesystem::disk('public')->putFileAs($module, Request::file('multi_pic'), $file->getOriginalName());
            return json_encode(['code' => 0, 'img' => $savename]);
        } else {
            if (!empty($param['rule'])) {
                return json_encode(['code' => 0, 'img' => uploadOneOrigin('file_data')]);
            } else {
                return json(['code' => 0, 'img' => uploadOne('single_pic')]);
            }

        }
    }


    public function getSignPackage(){
        $ret = (new JSSDK())->getSignPackage();


        return json($ret);
    }

    /**
     * @param $table
     * @param $id
     * @return mixed
     * 查询一条数据
     */
    public function find($table,$id)
    {
        $class = 'app\model\\' . $table;
        $data = (new $class)->find($id);
        return $data;
    }

    /**
     * @param $table
     * @param $id
     * @param $field
     * @return mixed
     * 查询单个字段数据;
     */
    public function value($table, $id, $field)
    {
        $class = 'app\model\\' . $table;
        $data = (new $class)->where('id', $id)->value($field);
        return $data;
    }

    public function select($table, $sort = true)
    {
        $class = 'app\model\\' . $table;
        if ($sort == true) {
            $data = (new $class)->order(['sort' => 'asc', 'id' => 'desc'])->select();
        } else {
            $data = (new $class)->order(['id' => 'desc'])->select();
        }
        return $data;
    }

    public function first_id($table){

    }

}